package com.sfl.api_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sfl.api_backend.entity.SecurityUser;
import com.sfl.api_backend.entity.Users;
import com.sfl.api_backend.service.IUsersService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author szc
 * @create 2023/2/2 20:13
 */
@Service
public class SflUserDetailService implements UserDetailsService {

    @Resource
    private IUsersService usersService;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        Wrapper<Users> wrapper = new LambdaQueryWrapper<>();
        ((LambdaQueryWrapper<Users>) wrapper).eq(Users::getEmail, email);
        Users users = usersService.getOne(wrapper);

        if(users == null){
            throw new UsernameNotFoundException("用户不存在");
        }

        return new SecurityUser(users);

    }




}
